package com.jch;


import com.entity.Movie;
import com.entity.User;
import com.hcy.Forget;
import com.hcy.MainFrame;
import com.hcy.Recharge;
import com.jch.Dao.MovieDao;
import com.jch.Dao.UserDao;
import com.wl.代码.Move_room;
import com.xlp.RegisterFrame;
import javafx.scene.input.KeyCode;


import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class Index extends JFrame {
    private JPanel panel;
    private JLabel toplable, label, username, sex, pwd, repwd, prompt, jl_forgetpsw;
    private JTextField usernameFid;
    private JPasswordField pwdFid, repwdFid;
    private JRadioButton mRbtn, wRbtn;
    private ButtonGroup buttonGroup;
    private ImageIcon icon = new ImageIcon(Index.class.getResource("1.jpg"));
    private JButton back, confirm;
    private Font font = new Font("宋体", Font.BOLD, 13);
    private boolean flag;



    private int i;

    private Index index;






    public Index() {




        setTitle("用户登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1086, 755);
        setLocationRelativeTo(null);
        setResizable(false);

        panel = (JPanel) getContentPane();
        panel.setOpaque(false);//将面板设置为透明
        label = new JLabel(icon);
        getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
        label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        panel.setLayout(null);


        toplable=new JLabel("电  影  购  票  系  统");
        toplable.setBounds(380,40,300,45);
        toplable.setFont(new Font("微软雅黑",Font.BOLD,22));
        toplable.setForeground(Color.black);
        panel.add(toplable);

        //用户名
        username = new JLabel("用户名:");
        username.setBounds(120, 100, 50, 28);
        username.setFont(font);
        username.setForeground(new Color(196, 255, 14));
        panel.add(username);



        usernameFid = new JTextField();
        usernameFid.setBounds(172, 102, 150, 23);
        usernameFid.setHorizontalAlignment(SwingConstants.CENTER);
        usernameFid.setFont(new Font("楷体", Font.BOLD, 13));
        panel.add(usernameFid);



        //密码
        pwd = new JLabel("密  码:");
        pwd.setBounds(120, 160, 70, 28);
        pwd.setForeground(new Color(196, 255, 14));
        pwd.setFont(font);

        pwdFid = new JPasswordField();
        pwdFid.setBounds(172, 162, 150, 23);
        panel.add(pwd);
        panel.add(pwdFid);


        mRbtn = new JRadioButton("用户", true);
        mRbtn.setBounds(150, 220, 80, 20);
        mRbtn.setBackground(Color.white);
        //选择点击事件
        mRbtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED){
                    i=0;
                }
            }
        });

        wRbtn = new JRadioButton("管理员");
        wRbtn.setBounds(248, 220, 80, 20);
        wRbtn.setBackground(Color.white);
        //选择点击事件
        wRbtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED){
                    i=1;
                }
            }
        });


        buttonGroup = new ButtonGroup();
        buttonGroup.add(mRbtn);
        buttonGroup.add(wRbtn);

        panel.add(mRbtn);
        panel.add(wRbtn);


        //忘记密码
        jl_forgetpsw = new JLabel("找回密码");
        jl_forgetpsw.setBounds(360, 278, 80, 28);
        jl_forgetpsw.setFont(font);
        jl_forgetpsw.setForeground(new Color(4,139,204));
        jl_forgetpsw.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jl_forgetpsw.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
             new Forget();
          }
      });
        panel.add(jl_forgetpsw);






        //按钮
        confirm = new JButton("注册");
        confirm.setBackground(Color.BLUE);
        confirm.setForeground(Color.YELLOW);
        confirm.setFocusPainted(false);
        confirm.setFont(font);
        confirm.setBounds(153, 278, 75, 32);
        panel.add(confirm);
        confirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                new RegisterFrame();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(new Cursor(12));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(new Cursor(0));
            }
        });

        back = new JButton("登录");
        back.setBackground(Color.BLUE);
        back.setFocusPainted(false);
        back.setForeground(Color.YELLOW);
        back.setFont(font);
        back.setBounds(255, 278, 75, 32);
        panel.add(back);

        //鼠标点击事件
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                User user=new User();
                UserDao userDao = new UserDao();
                  //调用方法查询数据库中的昵称和密码
                    List<User> users = userDao.queryUsersById(usernameFid.getText(), pwdFid.getText());
                    for (User user1 : users) {
                        System.out.println(users);
                        user.setuName(usernameFid.getText());
                        user.setuPass(pwdFid.getText());
                        user.setuType(user1.getuType());
                        user.setUid(user1.getUid());
                    }



                //判断文本域中输入的值是否与数据库中的值是否相同
                if(usernameFid.getText().equals(user.getuName()) &&
                        pwdFid.getText().equals(user.getuPass()) &&
                      i==user.getuType()   ){


                    Toolkit.getDefaultToolkit().beep();
                    if (i==0){
                        new Move_room(usernameFid);
                        dispose();
                    }else if (i == 1){
                        new MainFrame();
                        dispose();
                    }




                } else {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(index, "用户名或密码错误");
                    return;
                }


            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(new Cursor(12));

            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(new Cursor(0));

            }

        });

        back.registerKeyboardAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User user=new User();
                UserDao userDao = new UserDao();
                //调用方法查询数据库中的昵称和密码
                List<User> users = userDao.queryUsersById(usernameFid.getText(), pwdFid.getText());
                for (User user1 : users) {
                    System.out.println(users);
                    user.setuName(usernameFid.getText());
                    user.setuPass(pwdFid.getText());
                    user.setuType(user1.getuType());
                    user.setUid(user1.getUid());
                }



                //判断文本域中输入的值是否与数据库中的值是否相同
                if(usernameFid.getText().equals(user.getuName()) &&
                        pwdFid.getText().equals(user.getuPass()) &&
                        i==user.getuType()   ){


                    Toolkit.getDefaultToolkit().beep();
                    new Move_room(usernameFid);

                    dispose();


                }
                else {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(index, "用户名或密码错误");
                    return;
                }

            }
        },KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);

        setVisible(true);

    }

    public static void main(String[] args) {

        new Index();
    }

}
